<template>
  <div class="app-container">
    <div style="margin-bottom: 80px">
      <!--        <el-radio  fill= ”#409EFF“ style="background-color: #909399;width: 200px;height: 100px;   display: inline-flex; align-items: center;justify-content: center;" v-model="symbol" label="1" border>备选项1</el-radio>-->
      <!--        <el-radio  fill= ”#409EFF“ style="background-color: #28d4c1;width: 200px;height: 100px;   display: inline-flex; align-items: center;justify-content: center;" v-model="symbol" label="2" border>备选项1</el-radio>-->


      <el-radio-group v-model="symbol">
        <el-radio-button size="medium" label="上海"></el-radio-button>
        <el-radio-button size="medium" label="北京"></el-radio-button>
        <el-radio-button size="medium" label="广州"></el-radio-button>
        <el-radio-button size="medium" label="深圳"></el-radio-button>
      </el-radio-group>


    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >买入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSale"
        >卖出
        </el-button>
      </el-col>

      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['system:record:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="股票代码" prop="stockCode">
        <el-input
          v-model="queryParams.stockCode"
          placeholder="请输入股票代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="策略id" prop="strategyId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.strategyId"-->
      <!--          placeholder="请输入策略id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="recordList">
      <!--      <el-table-column label="策略ID" align="center" prop="strategyId"/>-->
      <el-table-column label="订单编号" width="310" align="center" prop="orderNo"/>
      <el-table-column label="股票代码" width="200" align="center" prop="stockCode"/>
      <el-table-column label="交易所" align="center" prop="exchange">
        <template slot-scope="scope">
          <span v-if="scope.row.exchange == 0">沪市</span>
          <span v-if="scope.row.exchange == 1">深市</span>
        </template>
      </el-table-column>
      <el-table-column label="委托类型" align="center" prop="delegateType">
        <template slot-scope="scope">
          <span v-if="scope.row.delegateType === 0">普通买入</span>
          <span v-if="scope.row.delegateType === 1">普通卖出</span>
          <span v-if="scope.row.delegateType === 2">信用卖出</span>
          <!--          <span v-if="scope.row.delegateType == 3">深市</span>-->
          <!--          <span v-if="scope.row.delegateType == 4">深市</span>-->
          <!--          <span v-if="scope.row.delegateType == 5">深市</span>-->
          <!--          <span v-if="scope.row.delegateType == 6">深市</span>-->
          <!--          <span v-if="scope.row.delegateType == 7">深市</span>-->
          <!--          <span v-if="scope.row.delegateType == 8">深市</span>-->

        </template>
      </el-table-column>
      <!--      <el-table-column label="委托状态" align="center" prop="delegateState" />-->

      <el-table-column label="委托状态" align="center" prop="delegateState">
        <template slot-scope="scope">
          <span v-if="scope.row.delegateState === 0">未申报</span>
          <span v-if="scope.row.delegateState === 1">正在申报</span>
          <span v-if="scope.row.delegateState === 2">已申报</span>
          <span v-if="scope.row.delegateState === 3">已报待撤</span>
          <span v-if="scope.row.delegateState === 4">部成待撤</span>
          <span v-if="scope.row.delegateState === 5">部撤</span>
          <span v-if="scope.row.delegateState === 6">已撤</span>
          <span v-if="scope.row.delegateState === 7">部分成交</span>
          <span v-if="scope.row.delegateState === 8">已成交</span>
          <span v-if="scope.row.delegateState === 9">废单</span>
          <span v-if="scope.row.delegateState === 10">错误</span>
          <span v-if="scope.row.delegateState === 11">其他未知</span>

        </template>
      </el-table-column>
      <el-table-column label="触发类型" align="center" prop="triggerType">
        <template slot-scope="scope">
          <span v-if="scope.row.triggerType == 'AUTO_TAKE_POSITION_TRIGGER'">自动开仓</span>
          <span v-if="scope.row.triggerType == 'STATIC_STOP_PROFIT_TRIGGER'">静态止盈</span>
          <span v-if="scope.row.triggerType == 'STATIC_STOP_LOSS_TRIGGER'">静态止损</span>
          <span v-if="scope.row.triggerType == 'DYNAMIC_STOP_LOSS_TRIGGER'">动态止损</span>
          <span v-if="scope.row.triggerType == 'DYNAMIC_STOP_PROFIT_TRIGGER'">动态止盈</span>
          <span v-if="scope.row.triggerType == 'TIME_CLOSE_POSITION_TRIGGER'">定时平仓</span>
        </template>
      </el-table-column>
      <el-table-column label="委托价格" align="center" prop="price"/>
      <el-table-column label="成交价格" align="center" prop="dealPrice"/>
      <el-table-column label="成交数量" align="center" prop="dealVolume"/>
      <el-table-column label="成交金额" align="center" prop="dealAmount"/>
      <el-table-column label="委托股数" align="center" prop="delegateVolume"/>
      <el-table-column label="委托开仓金额" align="center" prop="delegateAmount"/>
      <!--      <el-table-column label="仓号" align="center" width="300"  prop="delegateCode"/>-->
      <el-table-column label="撤单委托 " align="center" prop="delegateCancle">
        <template slot-scope="scope">
          <span v-if="scope.row.delegateCancle === true">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column label="单据状态" align="center" prop="tradingStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.tradingStatus === false">交易失败</span>
          <span v-if="scope.row.tradingStatus === true">交易成功</span>
        </template>
      </el-table-column>
      <el-table-column label="失败消息 " width="100" align="center" prop="errorMsg"/>
      <!--      <el-table-column label="平仓收益金额 " width="100" align="center" prop="profit"/>-->
      <!--      <el-table-column label="收益率 " align="center" prop="profitRate"/>-->
      <el-table-column label="撤单数量" align="center" prop="delegateCancleVolume"/>
      <el-table-column label="交易时间" align="center" prop="createTime" width="220"/>

      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleCancelUpdate(scope.row)"-->
      <!--            v-hasPermi="['system:record:edit']"-->
      <!--          >撤单</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-sell"-->
      <!--            @click="handleSellUpdate(scope.row)"-->
      <!--            v-hasPermi="['system:record:edit']"-->
      <!--          >卖出</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手工策略配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="股票代码" prop="strategyId">
          <el-select v-model="form.strategyId" placeholder="请选择标的" clearable>
            <el-option
              v-for="item in manualConfigList"
              :key="item.id"
              :label="item.stockCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="开仓金额" prop="delegateAmount">
          <el-input v-model="form.delegateAmount" placeholder="请输入开仓金额"/>
        </el-form-item>
        <!--        <el-form-item label="委托股数" prop="delegateVolume">-->
        <!--          <el-input v-model="form.delegateVolume" placeholder="请输入委托股数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="委托编号" prop="delegateCode">-->
        <!--          <el-input v-model="form.delegateCode" placeholder="请输入委托编号" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="撤单委托: 0:false 1：true" prop="delegateCancle">-->
        <!--          <el-input v-model="form.delegateCancle" placeholder="请输入撤单委托: 0:false 1：true" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="撤单数量" prop="delegateCancleVolume">-->
        <!--          <el-input v-model="form.delegateCancleVolume" placeholder="请输入撤单数量" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="是否平仓(0:否 1：是)" prop="positionClosed">-->
        <!--          <el-input v-model="form.positionClosed" placeholder="请输入是否平仓(0:否 1：是)" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="策略id" prop="strategyId">-->
        <!--          <el-input v-model="form.strategyId" placeholder="请输入策略id" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="平仓盈亏" prop="profit">-->
        <!--          <el-input v-model="form.profit" placeholder="请输入平仓盈亏" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="收益率" prop="profitRate">-->
        <!--          <el-input v-model="form.profitRate" placeholder="请输入收益率" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="错误信息" prop="errorMsg">-->
        <!--          <el-input v-model="form.errorMsg" placeholder="请输入错误信息" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="创建人员" prop="createUser">-->
        <!--          <el-input v-model="form.createUser" placeholder="请输入创建人员" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="this.submitLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.custom-size-radio >>> .el-radio__inner {
  width: 24px !important; /* 自定义宽度 */
  height: 24px !important; /* 自定义高度 */
}


</style>
<script>
import {listRecord, getRecord, delRecord, addRecord, updateRecord} from "@/api/manual/record";
import {listConfig} from "@/api/manual/config";

import {getDicts} from "@/api/system/dict/data";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      submitLoading: false,
      symbol: null,
      symbol1: null,

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 手工策略配置表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      manualConfigList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        stockCode: null,
        exchange: null,
        delegateType: null,
        delegateState: null,
        price: null,
        dealPrice: null,
        dealVolume: null,
        dealAmount: null,
        delegateVolume: null,
        delegateCode: null,
        tradingStatus: null,
        delegateCancle: null,
        delegateCancleVolume: null,
        triggerType: null,
        positionClosed: null,
        strategyId: null,
        profit: null,
        profitRate: null,
        errorMsg: null,
        createUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getManualConfigList();

  },
  methods: {
    /** 查询手工策略配置列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询手工策略列表 */
    async getManualConfigList() {
      await listConfig({"status": 0}).then(response => {
        console.log("manualConfigList:", response)
        this.manualConfigList = response.rows;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        stockCode: null,
        exchange: null,
        delegateType: null,
        delegateState: null,
        price: null,
        dealPrice: null,
        dealVolume: null,
        dealAmount: null,
        delegateVolume: null,
        delegateAmount: null,
        delegateCode: null,
        tradingStatus: null,
        delegateCancle: null,
        delegateCancleVolume: null,
        triggerType: null,
        positionClosed: null,
        strategyId: null,
        profit: null,
        profitRate: null,
        errorMsg: null,
        createTime: null,
        createUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 买入按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      this.form.delegateType = 0;
      this.title = "买入委托";
    },

    /** 卖出按钮操作 */
    handleSale() {
      this.reset();
      // this.open = true;
      this.form.delegateType = 1;
      this.title = "卖出委托";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改手工策略配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.form.id == null) {
          this.submitLoading = true
          addRecord(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.submitLoading = false;
            this.open = false;
            this.getList();
          }).catch(err => {
            this.submitLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除手工策略配置编号为"' + ids + '"的数据项？').then(function () {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
